package com.slkj.shilixiaoyuanapp.ui.teacher.tool.goods;

import android.text.TextUtils;
import android.widget.EditText;
import android.widget.TextView;

import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.WplySqrandBmEntity;
import com.slkj.shilixiaoyuanapp.entity.code.ToolCode;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.ui.teacher.tool.common.CommonToolHistoryActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.util.TimeUtils;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.PickerViewProvider;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.tkk.api.RxEventProcessor;
import com.tkk.api.annotation.Recipient;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SPR_FINSH;
import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.NO_CHOSE;

@ActivityInfo(layout = R.layout.activity_aplyy_goodsk, title = "物品领用", rightHint = "历史记录")
public class AplyyGoodsActivity extends BaseActivity {
    @BindView(R.id.tv_spr)
    TextView tvSpr;
    @BindView(R.id.tv_bm)
    TextView tvBm;
    @BindView(R.id.edit_name)
    EditText editName;
    @BindView(R.id.edit_num)
    EditText editNum;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_now_day_time)
    TextView tvNowDayTime;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    List<Integer> sqrIds = new ArrayList<>();
    private int bmTypeId = NO_CHOSE;
    private ArrayList<PeopleEntity> peopleEntities = new ArrayList<>();
    WplySqrandBmEntity choseData;
    public static final String tag = "AplyyGoodsActivity";

    @Override
    protected void init() {
        RxEventProcessor.get().bind(this);
        tvUserName.setText(UserRequest.getInstance().getUser().getUser().getUName());
        tvNowDayTime.setText(TimeUtils.getNowNyr());
    }

    @Override
    protected void onRightTitleClick() {
        CommonToolHistoryActivity__JumpCenter
                .builder(this)
                .setNetToolType(2)
                .setToolType(ToolCode.TOOL_WPLY)
                .create().go();
    }


    @OnClick(R.id.tv_up)
    void upData() {
        String name = editName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请输入领用物品名称");
            return;
        }

        String num = editNum.getText().toString();
        if (TextUtils.isEmpty(num)) {
            showToast("请输入领用物品数量");
            return;
        }

        if (bmTypeId == NO_CHOSE) {
            showToast("请选择使用部门");
            return;
        }

        String context = editContent.getText();
        if (TextUtils.isEmpty(context)) {
            showToast("请输入领用事由");
            return;
        }
        if (sqrIds.size() == 0) {
            showToast("请选择审批人");
            return;
        }

        HttpHeper.get().toolService()
                .addWply(name,sqrIds.toString(),bmTypeId,num,context)
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(AplyyGoodsActivity.this,data);
                        clearContent();
                    }
                });
    }

    /**
     * 获取选择数据
     *
     * @param type 1：部门 2：申请人
     */
    private void getChoseData(int type) {
        HttpHeper.get().toolService()
                .getWplySqr()
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<WplySqrandBmEntity>(true, this) {
                    @Override
                    public void onCallBackSuccess(WplySqrandBmEntity data) {
                        choseData = data;
                        peopleEntities.addAll(choseData.getAuditorVo());
                        if (type == 1) {
                            showTypePicker(choseData.getDepartment());
                        } else {
                            gotoChosePeople();
                        }
                    }
                });
    }

    @OnClick(R.id.layoout_bumen)
    public void onLayoutChoseBmClicked() {
        if (choseData == null) {
            getChoseData(1);
        } else {
            showTypePicker(choseData.getDepartment());
        }

    }

    private void showTypePicker(List<WplySqrandBmEntity.DepartmentEntity> data) {
        PickerViewProvider.getCommonPicker(this, data, (options1, options2, options3, v) -> {
            tvBm.setText(data.get(options1).getDName());
            bmTypeId = data.get(options1).getDId();
        }).show();
    }

    private void gotoChosePeople() {
        ChosePeopleActivity__JumpCenter
                .builder(this)
                .setData(peopleEntities)
                .setTag(tag)
                .create()
                .go();
    }

    @OnClick(R.id.layout_chose_spr)
    public void onLayoutChoseSprClicked() {
        if (choseData == null) {
            getChoseData(2);
        } else {
            gotoChosePeople();
        }
    }

    @Recipient(tag = tag + CHOSE_SPR_FINSH)
    void onChoseObjFinsh(int code, String info, List<PeopleEntity> peopleEntityList, List<Integer> checkIds) {
        tvSpr.setText(checkIds.size() == 0 ? "" : info);
        sqrIds.clear();
        sqrIds.addAll(checkIds);
        peopleEntities.clear();
        peopleEntities.addAll(peopleEntityList);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxEventProcessor.get().unBind(this);
    }

}
